package com.dqp.cslggroup.UI;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.dqp.cslggroup.BuildConfig;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.greendao.DaoMaster;
import com.dqp.cslggroup.greendao.DaoSession;
import org.greenrobot.greendao.database.Database;

/**
 * 获取全局context
 */
public class MyApplication extends Application {

    private static Context context;
    private static DaoSession daoSession;

    @SuppressLint("MissingSuperCall")
    @Override
    public void onCreate() {
        context = getApplicationContext();
        setupDatabase();
    }

    public static Context getContext() {
        return context;
    }


    /**
     * 配置数据库
     */
    private void setupDatabase() {
        //创建数据库shop.db
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "cslg.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);

        boolean frist = SharedPreferencesUtil.getBooleanSp(context, BuildConfig.VERSION_NAME, true);

        if (frist) {
            DaoMaster.dropAllTables(daoMaster.getDatabase(), true);
            DaoMaster.createAllTables(daoMaster.getDatabase(), true);
            SharedPreferencesUtil.putBooleanSp(this,BuildConfig.VERSION_NAME,false);
        }

        //获取dao对象管理者
        daoSession = daoMaster.newSession();

    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }


}
